//
//  WmcCloudAllPhotoDayCell.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/5/30.
//

import UIKit

// MARK: - [WmcCloudAllPhotoDayCell]

class WmcCloudAllPhotoDayCell: WmcBaseTableCell {
    // Internal

    var clickImage: ((WmcMediaPhotoModel, UIImage?) -> Void)?

    override func makeUI() {
        addSubview(collectionView)

        collectionView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalToSuperview()
        }
    }

    func bindModel(_ model: WmcMediaPhotoDayModel) {
        dataSource = model.placeList
        collectionView.reloadData()
    }
    
    // Private

    private var dataSource: [WmcMediaPhotoPlaceModel] = []

    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let width = (UIScreen.main.bounds.width - 38) / 3.0
        layout.itemSize = CGSize(width: width, height: width)
        layout.minimumLineSpacing = 3
        layout.minimumInteritemSpacing = 3
        layout.sectionInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
        layout.sectionHeadersPinToVisibleBounds = false

        let collectView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectView.backgroundColor = UIColor.wmc_white_FFFFFF
        collectView.delegate = self
        collectView.dataSource = self
        collectView.showsVerticalScrollIndicator = false
        collectView.register(WmcCloudPhotoCell.self, forCellWithReuseIdentifier: "WmcCloudPhotoCell")
        collectView.register(WmcCloudAllPhotoFooterView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "WmcCloudAllPhotoFooterView")
        collectView.isScrollEnabled = false
        return collectView
    }()
}

// MARK: - UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout

extension WmcCloudAllPhotoDayCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return dataSource.count
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let place = dataSource[section]
        return place.photoList.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WmcCloudPhotoCell", for: indexPath) as! WmcCloudPhotoCell
        if let place = dataSource.yp.object(at: indexPath.section), let model = place.photoList.yp.object(at: indexPath.row) {
            cell.bindModel(model)
        }
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionFooter {
            let headerView: WmcCloudAllPhotoFooterView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "WmcCloudAllPhotoFooterView", for: indexPath) as! WmcCloudAllPhotoFooterView
            if let model = dataSource.yp.object(at: indexPath.section) {
                headerView.bindModel(model)
            }
            return headerView
        } else {
            return .init()
        }
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: 0.0, height: 0.0)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize(width: UIScreen.main.bounds.width, height: 41)
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard let cell = collectionView.cellForItem(at: indexPath) as? WmcCloudPhotoCell else { return }

        let place = dataSource[indexPath.section]
        let model = place.photoList[indexPath.row]

        clickImage?(model, cell.imageView.image)
    }
}
